package net.office.controller;

import net.office.bean.AnchorInfo;
import net.office.bean.BaseDict;
import net.office.bean.StaffInfo;
import net.office.service.AnchorInfoService;
import net.office.service.BaseDictService;
import net.office.service.LoginService;
import net.office.utils.TemplateParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class AnchorInfoController {

    @Autowired
    private AnchorInfoService anchorInfoService;

    @Autowired
    private BaseDictService baseDictService;

    @Autowired
    private LoginService loginService;

    /*查询全部列表*/
    @RequestMapping("/anchor/all/list.do")
    public String allList(Model model){
        List<AnchorInfo> anchorInfoList = anchorInfoService.findAll();

        for(int i=0; i<anchorInfoList.size(); i++){
            if(anchorInfoList.get(i).getAnchorSex() != null) {
                anchorInfoList.get(i).setSexDict(baseDictService.findOneByDictId(anchorInfoList.get(i).getAnchorSex()));
            }
            if(anchorInfoList.get(i).getAnchorType() != null) {
                anchorInfoList.get(i).setAnchorTypeDict(baseDictService.findOneByDictId(anchorInfoList.get(i).getAnchorType()));
            }
            if(anchorInfoList.get(i).getAnchorLevel() != null) {
                anchorInfoList.get(i).setAnchorLevelDict(baseDictService.findOneByDictId(anchorInfoList.get(i).getAnchorLevel()));
            }
            if(anchorInfoList.get(i).getAnchorStatus() != null) {
                anchorInfoList.get(i).setAnchorStatusDict(baseDictService.findOneByDictId(anchorInfoList.get(i).getAnchorStatus()));
            }
        }
        model.addAttribute("anchorInfoList", anchorInfoList);

        List<BaseDict> sexList = baseDictService.findByDictTypeCode(TemplateParam.SEX_CODE);
        List<BaseDict> anchorTypeList = baseDictService.findByDictTypeCode(TemplateParam.ANCHOR_TYPE_CODE);
        List<BaseDict> anchorLevelList = baseDictService.findByDictTypeCode(TemplateParam.ANCHOR_LEVEL_CODE);
        List<BaseDict> anchorStatusList = baseDictService.findByDictTypeCode(TemplateParam.ANCHOR_STATUS_CODE);
        model.addAttribute("sexList", sexList);
        model.addAttribute("anchorTypeList", anchorTypeList);
        model.addAttribute("anchorLevelList", anchorLevelList);
        model.addAttribute("anchorStatusList", anchorStatusList);

        return "artist";
    }

    /*搜索全部列表*/
    @RequestMapping("/anchor/all/search.do")
    public String allSearch(@RequestParam(value = "anchorNo", required = false) String anchorNo,
                            @RequestParam(value = "anchorName", required = false) String anchorName,
                            @RequestParam(value = "anchorSex", required = false) Integer anchorSex,
                            @RequestParam(value = "anchorType", required = false) Integer anchorType,
                            @RequestParam(value = "anchorLevel", required = false) Integer anchorLevel,
                            @RequestParam(value = "anchorStatus", required = false) Integer anchorStatus,
                            @RequestParam(value = "staffName", required = false) String staffName,
                            Model model){

        AnchorInfo anchorInfo = new AnchorInfo();

        if(anchorNo!=null && anchorNo.trim().length()!=0){
            anchorInfo.setStaffNo(anchorNo);
        }
        if(anchorName!=null && anchorName.trim().length()!=0){
            anchorInfo.setAnchorName(anchorName);
        }
        if(anchorSex!=null){
            anchorInfo.setAnchorSex(anchorSex);
        }
        if(anchorType!=null){
            anchorInfo.setAnchorType(anchorType);
        }
        if(anchorLevel!=null){
            anchorInfo.setAnchorLevel(anchorLevel);
        }
        if(anchorStatus!=null){
            anchorInfo.setAnchorStatus(anchorStatus);
        }
        if(staffName!=null && staffName.trim().length()!=0){
            anchorInfo.setStaffName(staffName);
        }

        List<AnchorInfo> anchorInfoList = anchorInfoService.findByAnchorInfo(anchorInfo);

        for(int i=0; i<anchorInfoList.size(); i++){
            if(anchorInfoList.get(i).getAnchorSex() != null) {
                anchorInfoList.get(i).setSexDict(baseDictService.findOneByDictId(anchorInfoList.get(i).getAnchorSex()));
            }
            if(anchorInfoList.get(i).getAnchorType() != null) {
                anchorInfoList.get(i).setAnchorTypeDict(baseDictService.findOneByDictId(anchorInfoList.get(i).getAnchorType()));
            }
            if(anchorInfoList.get(i).getAnchorLevel() != null) {
                anchorInfoList.get(i).setAnchorLevelDict(baseDictService.findOneByDictId(anchorInfoList.get(i).getAnchorLevel()));
            }
            if(anchorInfoList.get(i).getAnchorStatus() != null) {
                anchorInfoList.get(i).setAnchorStatusDict(baseDictService.findOneByDictId(anchorInfoList.get(i).getAnchorStatus()));
            }
        }
        model.addAttribute("anchorInfoList", anchorInfoList);

        List<BaseDict> sexList = baseDictService.findByDictTypeCode(TemplateParam.SEX_CODE);
        List<BaseDict> anchorTypeList = baseDictService.findByDictTypeCode(TemplateParam.ANCHOR_TYPE_CODE);
        List<BaseDict> anchorLevelList = baseDictService.findByDictTypeCode(TemplateParam.ANCHOR_LEVEL_CODE);
        List<BaseDict> anchorStatusList = baseDictService.findByDictTypeCode(TemplateParam.ANCHOR_STATUS_CODE);
        model.addAttribute("sexList", sexList);
        model.addAttribute("anchorTypeList", anchorTypeList);
        model.addAttribute("anchorLevelList", anchorLevelList);
        model.addAttribute("anchorStatusList", anchorStatusList);

        return "artist";
    }

    /*新增*/
    @RequestMapping("/anchor/insert.do")
    public String insert(@RequestParam(value = "anchorNo", required = false) String anchorNo,
                         @RequestParam(value = "anchorName", required = false) String anchorName,
                         @RequestParam(value = "anchorPassword", required = false) String anchorPassword,
                         @RequestParam(value = "anchorSex", required = false) Integer anchorSex,
                         @RequestParam(value = "anchorBirthday", required = false) Date anchorBirthday,
                         @RequestParam(value = "anchorIdCard", required = false) String anchorIdCard,
                         @RequestParam(value = "anchorType", required = false) Integer anchorType,
                         @RequestParam(value = "anchorLevel", required = false) Integer anchorLevel,
                         @RequestParam(value = "anchorStatus", required = false) Integer anchorStatus,
                         @RequestParam(value = "staffNo", required = false) String staffNo,
                         @RequestParam(value = "staffName", required = false) String staffName,
                         HttpSession session, Model model){

        //检查员工是否存在，工号、姓名是否保持一致,...
        StaffInfo staffInfo = loginService.findByStaffNo(staffNo);
        if(staffInfo == null){
            //model.addAttribute("msg", "staff");
            return "redirect:/anchor/toAdd.do";
        }else if(!staffInfo.getStaffName().equals(staffName)){
            //model.addAttribute("msg", "staff");
            return "redirect:/anchor/toAdd.do";
        }

        AnchorInfo anchorInfo = new AnchorInfo();

        anchorInfo.setStaffId(staffInfo.getStaffId());
        anchorInfo.setStaffNo(staffInfo.getStaffNo());
        anchorInfo.setStaffName(staffInfo.getStaffName());
        if(anchorNo!=null && anchorNo.trim().length()!=0){
            anchorInfo.setAnchorNo(anchorNo);
        }
        if(anchorName!=null && anchorName.trim().length()!=0){
            anchorInfo.setAnchorName(anchorName);
        }
        if(anchorPassword!=null && anchorPassword.trim().length()!=0){
            anchorInfo.setAnchorPassword(anchorPassword);
        }
        if(anchorSex!=null){
            anchorInfo.setAnchorSex(anchorSex);
        }
        if(anchorBirthday!=null){
            anchorInfo.setAnchorBirthday(anchorBirthday);
        }
        if(anchorIdCard!=null && anchorIdCard.trim().length()!=0){
            anchorInfo.setAnchorIdCard(anchorIdCard);
        }
        if(anchorType!=null){
            anchorInfo.setAnchorType(anchorType);
        }
        if(anchorLevel!=null){
            anchorInfo.setAnchorLevel(anchorLevel);
        }
        if(anchorStatus!=null){
            anchorInfo.setAnchorStatus(anchorStatus);
        }

        int res = anchorInfoService.insert(anchorInfo);

        if(res > 0){
            if(true){
                return "redirect:/anchor/all/list.do";
            }else{
                return "redirect:/anchor/person/list.do";
            }

        }

        return "anchor-add";
    }

    /*修改*/
    @RequestMapping("/anchor/update.do")
    public String update(@RequestParam(value = "anchorId", required = false) Integer anchorId,
                         @RequestParam(value = "anchorNo", required = false) String anchorNo,
                         @RequestParam(value = "anchorName", required = false) String anchorName,
                         @RequestParam(value = "anchorPassword", required = false) String anchorPassword,
                         @RequestParam(value = "anchorSex", required = false) Integer anchorSex,
                         @RequestParam(value = "anchorBirthday", required = false) Date anchorBirthday,
                         @RequestParam(value = "anchorIdCard", required = false) String anchorIdCard,
                         @RequestParam(value = "anchorType", required = false) Integer anchorType,
                         @RequestParam(value = "anchorLevel", required = false) Integer anchorLevel,
                         @RequestParam(value = "anchorStatus", required = false) Integer anchorStatus,
                         @RequestParam(value = "staffNo", required = false) String staffNo,
                         @RequestParam(value = "staffName", required = false) String staffName,
                         Model model){

        //检查员工是否存在，工号、姓名是否保持一致,...
        StaffInfo staffInfo = loginService.findByStaffNo(staffNo);
        if(staffInfo == null){
            //model.addAttribute("msg", "staff");
            return "redirect:/anchor/toEdit.do";
        }else if(!staffInfo.getStaffName().equals(staffName)){
            //model.addAttribute("msg", "staff");
            return "redirect:/anchor/toEdit.do";
        }

        AnchorInfo anchorInfo = new AnchorInfo();

        anchorInfo.setAnchorId(anchorId);
        anchorInfo.setStaffId(staffInfo.getStaffId());
        anchorInfo.setStaffNo(staffInfo.getStaffNo());
        anchorInfo.setStaffName(staffInfo.getStaffName());
        if(anchorNo!=null && anchorNo.trim().length()!=0){
            anchorInfo.setAnchorNo(anchorNo);
        }
        if(anchorName!=null && anchorName.trim().length()!=0){
            anchorInfo.setAnchorName(anchorName);
        }
        if(anchorPassword!=null && anchorPassword.trim().length()!=0){
            anchorInfo.setAnchorPassword(anchorPassword);
        }
        if(anchorSex!=null){
            anchorInfo.setAnchorSex(anchorSex);
        }
        if(anchorBirthday!=null){
            anchorInfo.setAnchorBirthday(anchorBirthday);
        }
        if(anchorIdCard!=null && anchorIdCard.trim().length()!=0){
            anchorInfo.setAnchorIdCard(anchorIdCard);
        }
        if(anchorType!=null){
            anchorInfo.setAnchorType(anchorType);
        }
        if(anchorLevel!=null){
            anchorInfo.setAnchorLevel(anchorLevel);
        }
        if(anchorStatus!=null){
            anchorInfo.setAnchorStatus(anchorStatus);
        }


        int res = anchorInfoService.updateByPrimaryKey(anchorInfo);

        if(res > 0){

        }

        return "redirect:/anchor/toEdit.do?anchorId="+anchorId+"";
    }

    /*删除单条(ajax)*/
    @RequestMapping("/anchor/deleteOne.do")
    @ResponseBody
    public Object deleteOne(@RequestParam("anchorId") Integer anchorId){

        int res = anchorInfoService.deleteByPrimaryKey(anchorId);

        Map<String, String> map = new HashMap<String, String>();
        if(res > 0){
            map.put("res", "1");
        }else{
            map.put("res", "0");
        }
        return map;
    }

    /*跳转到新增页面*/
    @RequestMapping("/anchor/toAdd.do")
    public String toAdd(Model model){

        List<BaseDict> sexList = baseDictService.findByDictTypeCode(TemplateParam.SEX_CODE);
        List<BaseDict> anchorTypeList = baseDictService.findByDictTypeCode(TemplateParam.ANCHOR_TYPE_CODE);
        List<BaseDict> anchorLevelList = baseDictService.findByDictTypeCode(TemplateParam.ANCHOR_LEVEL_CODE);
        List<BaseDict> anchorStatusList = baseDictService.findByDictTypeCode(TemplateParam.ANCHOR_STATUS_CODE);
        model.addAttribute("sexList", sexList);
        model.addAttribute("anchorTypeList", anchorTypeList);
        model.addAttribute("anchorLevelList", anchorLevelList);
        model.addAttribute("anchorStatusList", anchorStatusList);

        return "artist-add";
    }
    /*跳转到修改页面*/
    @RequestMapping("/anchor/toEdit.do")
    public String toEdit(@RequestParam("anchorId") Integer anchorId, Model model){
        AnchorInfo anchorInfo = anchorInfoService.findOneByAnchorInfoId(anchorId);
        model.addAttribute("anchorInfo", anchorInfo);

        List<BaseDict> sexList = baseDictService.findByDictTypeCode(TemplateParam.SEX_CODE);
        List<BaseDict> anchorTypeList = baseDictService.findByDictTypeCode(TemplateParam.ANCHOR_TYPE_CODE);
        List<BaseDict> anchorLevelList = baseDictService.findByDictTypeCode(TemplateParam.ANCHOR_LEVEL_CODE);
        List<BaseDict> anchorStatusList = baseDictService.findByDictTypeCode(TemplateParam.ANCHOR_STATUS_CODE);
        model.addAttribute("sexList", sexList);
        model.addAttribute("anchorTypeList", anchorTypeList);
        model.addAttribute("anchorLevelList", anchorLevelList);
        model.addAttribute("anchorStatusList", anchorStatusList);

        return "artist-edit";
    }
}
